package com.platform.hjt.base.handle;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import com.alibaba.fastjson.JSONObject;

public class MyExceptionHandler extends SimpleMappingExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception exception) {
		
		System.out.println("---系统异常处理....");
		
		try {
			
			if(request.getHeader("x-requested-with") != null
					&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
	
				response.setContentType("application/json;charset=UTF-8");
				
				JSONObject result = new JSONObject();
				PrintWriter out = response.getWriter();
				result.put("result", false);
				result.put("msg", "系统异常，请联系管理员");
				//result.put("url", request.getContextPath() + LOGIN_URL );
				
				out.print(result);
				out.flush();
				out.close();
			}else{
				//pw.write("<script>top.window.location.href = '"+ request.getContextPath() + LOGIN_URL+"?r='+Math.random() ;</script>");
				/*out.flush();
				out.close();*/
				response.setStatus(404);
				response.sendRedirect( request.getContextPath() + "/404.html");
			}
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
